package fun.werfamily.wf.user.core.controller;

import fun.werfamily.wf.base.dto.LoginUserContext;
import fun.werfamily.wf.base.vo.Result;
import fun.werfamily.wf.user.core.domain.dto.resp.UserAddressDTO;
import fun.werfamily.wf.user.core.domain.dto.resp.UserAddressIdDTO;
import fun.werfamily.wf.user.core.domain.service.IUserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "用户收货地址模块")
@RestController
@RequestMapping("/user/address")
public class UserAddressApiController {

    @Resource
    private IUserAddressService iUserAddressService;

    @PostMapping("/list")
    @ApiOperation("收货地址列表")
    public Result<List<UserAddressDTO>> list() {
        return Result.success(iUserAddressService.listByUserId(LoginUserContext.getUserCode()));
    }

    @PostMapping("/save")
    @ApiOperation("新增收货地址")
    public Result<Boolean> save(@Validated @RequestBody UserAddressDTO userAddressDTO) {
        userAddressDTO.setUserCode(LoginUserContext.getUserCode());
        return Result.success(iUserAddressService.saveAddress(userAddressDTO));
    }

    @PostMapping("/detail")
    @ApiOperation("收货地址详情")
    public Result<UserAddressDTO> getById(@Validated @RequestBody UserAddressIdDTO userAddressIdDTO) {
        userAddressIdDTO.setUserCode(LoginUserContext.getUserCode());
        return Result.success(iUserAddressService.getDetailById(userAddressIdDTO));
    }

    @PostMapping("/delete")
    @ApiOperation("删除收货地址")
    public Result<Boolean> delete(@Validated @RequestBody UserAddressIdDTO userAddressIdDTO) {
        userAddressIdDTO.setUserCode(LoginUserContext.getUserCode());
        return Result.success(iUserAddressService.deleteAddress(userAddressIdDTO));
    }

    @PostMapping("/update")
    @ApiOperation("更新收货地址")
    public Result<Boolean> updateById(@Validated @RequestBody UserAddressDTO userAddressDTO) {
        userAddressDTO.setUserCode(LoginUserContext.getUserCode());
        return Result.success(iUserAddressService.updateAddress(userAddressDTO));
    }

    @PostMapping("/getDefaultAddr")
    @ApiOperation("获取默认收货地址")
    public Result<UserAddressDTO> getDefaultAddr() {;
        return Result.success(iUserAddressService.getDefault(LoginUserContext.getUserCode()));
    }

    @PostMapping("/defaultAddr")
    @ApiOperation("设置默认地址")
    public Result<Boolean> defaultAddr(@RequestBody UserAddressIdDTO userAddressIdDTO) {
        userAddressIdDTO.setUserCode(LoginUserContext.getUserCode());
        return Result.success(iUserAddressService.setDefault(userAddressIdDTO));
    }
}
